// subpages0/subscribeNews/subscribeNews.js
const app = getApp();
import { request } from '../../utils/request.js'
Page({
  data: {

  },
  onLoad: function (e) {
    var that = this;
    if (e && e.title) {
      app.setNavtitle(e.title);
    } else {
      app.setNavtitle('订阅消息');
    }
    app.setNavColor(that);
    that.getSubScriptNews();
  },
  //订阅消息列表
  getSubScriptNews: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    request({
      data: {
        map:'applet_subscribe_tpl_list'
      },
      pointer: { page: this, fname: "getSubScriptNews" },
    }).then(res => {
      console.log('订阅消息列表', res.data.data);
      that.setData({
        newsList: res.data.data
      })
    }).catch(err => {
      console.log(err);
      that.setData({
        newsList:[]
      })
    })
  },
  toAgreeTmpl:function(e){
    var that = this;
    var tplId = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index; 
    var newsList = that.data.newsList;
    if (wx.canIUse('requestSubscribeMessage')) {
      wx.requestSubscribeMessage({
        tmplIds: tplId,
        success(res) {
          console.log('调起成功', res);
          var ids = [];
          var refuseId = [];
          for (let key in res) {
            console.log(key, res[key]);
            if (res[key] == 'accept') {
              ids.push(key);
            }
            if (res[key] == 'reject') {
              refuseId.push(key);
            }
          }
          if (ids.length > 0) {
            var num = Number(newsList[index].num);
            num++;
            that.setData({
              [`newsList[${index}].num`]: num
            })
            let tmplArr = {};
            tmplArr[tplId] = 1;
            that.saveAgreeTmpl(tmplArr); //存储同意的ids
          }
          if (refuseId.length > 0) {
            wx.showModal({
              title: '提示',
              content: '您拒绝了订阅消息提醒，是否到设置页面允许订阅？',
              success(res) {
                if (res.confirm) {
                  console.log('用户点击确定');
                  wx.openSetting({
                    success(res) {
                      console.log(res.authSetting)
                    }
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        },
        fail() {
          console.log('调起失败');
        },
        complete() {

        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '您的微信版本过低，暂不支持此功能，请升级微信版本！',
        success(res) {
          if (res.confirm) {
            console.log('用户点击确定');
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  },
  saveAgreeTmpl: function (tmplArr){
    wx.request({
      url: app.globalData.requestUrl,
      data: {
        map: 'applet_subscribe_save_auth',
        tplIds: JSON.stringify(tmplArr)
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          console.log('保存订阅消息成功');
          wx.showToast({
            title: res.data.data.msg,
            icon: 'success',
            duration: 2000
          })
        } else {
          console.log('保存订阅消息失败1');
        }
      },
      fail: function (res) {
        console.log('保存订阅消息失败2');
      },
      complete: function () {
        
      }
    });
  }
})